<?php
namespace App\Services;

use App\Model\Admin;
use App\Model\MaterialAssign;
use App\Repositories\AdminInfoRepository;
use App\Repositories\AdminRepository;
use App\Repositories\CommunityRepository;
use App\Repositories\CompanyUnitRepository;
use App\Repositories\StreetRepository;
use App\Traits\ReturnTrait;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Phper666\JwtAuth\Jwt;

class AdminService
{
    use ReturnTrait;
    /**
     * @Inject
     * @var Jwt
     */
    protected $jwt;

    public function login($data)
    {
        $username = $data['username'];
        $password = $data['password'];
        $admin = Admin::where('username', '=', $username)->first();
        if (!$admin) {
            return $this->error('管理员不存在');
        }
        if (!password_verify($password,$admin->password)) {
            return $this->error('账号密码不匹配');
        }
        if(!$admin->status){
            return $this->error('账号正在审核');
        }
        $admin->unit_num=null;
        if($admin->unit_id){
            $unit = CompanyUnitRepository::getInstance()->getById($admin->unit_id);
            $admin->unit_name = object_get($unit,'name',null);
        }
        $token = (string) $this->jwt->getToken(['admin_id' => $admin->id]);
        return $this->success([
            'admin' => $admin,
            'access_token' => $token,
            'expires_in' => $this->jwt->getTTL()
        ]);
    }

    public function register($data)
    {
        $username = $data['username'];
        $admin = Admin::where('username', '=', $username)->first();
        if ($admin) {
            return $this->error('用户名已存在');
        }
        $data['id'] = 0;
        $data['status'] = 0;
        $area_ids = explode(',',$data['area_ids']);
        if(count($area_ids)==1){
            $data['role_id'] = 3;
            $data['district_id'] = $area_ids[0];
        }elseif(count($area_ids)==2){
            $data['role_id'] = 4;
            $data['district_id'] = $area_ids[0];
            $data['street_id'] = $area_ids[1];
        }elseif(count($area_ids)==3){
            $data['role_id'] = 8;
            $data['district_id'] = $area_ids[0];
            $data['street_id'] = $area_ids[1];
            $data['community_id'] = $area_ids[2];
        }
        DB::beginTransaction();
        try{
            $result = AdminRepository::getInstance()->store($data);
            if(!$result['success']){
                throw new \Exception('信息保存失败');
            }
            $admin_id = $result['data']->id;
            $info = AdminInfoRepository::getInstance()->one(['id'],[[
                'field' => 'admin_id',
                'value' => $admin_id
            ]]);
            $basic_params=[
                'admin_id' => $admin_id,
                'id' => $info?$info->id:0,
                'contact_tel' => $data['contact_tel'],
                'real_name' => $data['real_name']
            ];
            $result_info = AdminInfoRepository::getInstance()->store($basic_params);
            if(!$result_info['success']){
                throw new \Exception('联系人信息保存失败');
            }
            DB::commit();
        }catch(\Exception $e){
            DB::rollBack();
            return $this->error($e->getMessage());
        }
        return $this->success([
        ]);
    }

    public function getFlowSelf($admin){
        $type=0;
        $data_id=0;
        if($admin->district_id){
            $type=MaterialAssign::TYPE_ONE;
            $data_id = $admin->district_id;
        }
        if($admin->street_id){
            $type = MaterialAssign::TYPE_TWO;
            $data_id = $admin->street_id;
        }
        if($admin->community_id){
            $type = MaterialAssign::TYPE_THREE;
            $data_id = $admin->community_id;
        }
        return [
            'type' => $type,
            'data_id' => $data_id
        ];
    }

    public function getFlowParent($admin){
        $type=0;
        $data_id = 0;
        if($admin->district_id){
            $type=MaterialAssign::TYPE_ONE;
            $data_id = $admin->district_id;
        }
        if($admin->street_id){
            $type = MaterialAssign::TYPE_TWO;
            $data_id = $admin->street_id;
        }
        if($admin->community_id){
            $type = MaterialAssign::TYPE_THREE;
            $data_id = $admin->community_id;
        }
        $return=[];
        switch ($type){
            case MaterialAssign::TYPE_ONE:
                    $return['parent_id']=0;
                    $return['type']=0;
                break;
            case MaterialAssign::TYPE_TWO:
                $street = StreetRepository::getInstance()->getById($data_id);
                $return['parent_id'] = $street->district_id;
                $return['type'] = MaterialAssign::TYPE_ONE;
                break;
            case MaterialAssign::TYPE_THREE:
                $community = CommunityRepository::getInstance()->getById($data_id);
                $return['parent_id'] = $community->street_id;
                $return['type'] = MaterialAssign::TYPE_TWO;
                break;
        }
        return $return;
    }
}
